import numpy as np
import pandas as pd
import matplotlib.pyplot as plt, seaborn as sns
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from sklearn.preprocessing import MinMaxScaler, RobustScaler
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, recall_score, f1_score
from sklearn import datasets
from sklearn import svm
import time
from sklearn.impute import SimpleImputer
from imblearn.over_sampling import SMOTE


start = time.time()
# 记录开始时间
start = time.time()

# 读取数据集
iris = datasets.load_iris()

# 准备特征和标签
iris_X = iris.data
iris_y = iris.target

# 数据预处理和拆分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(iris_X, iris_y, train_size=.7)

print(X_train.shape)

# if(missing_values):
#     # not used since there is none missing value in creditcard dataset
#     imputer = SimpleImputer(strategy='mean')
#     X_train_imputed = imputer.fit_transform(x_train)
#     X_test_imputed = imputer.transform(x_test) 

# X_train_resampled = MinMaxScaler().fit_transform(X_train_resampled)
# x_test = MinMaxScaler().fit_transform(x_test)

# model training and prediction
clf = svm.SVC(
    C=0.8,
    gamma=3,
    kernel='rbf',
    decision_function_shape='ovo'
)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
end_time = time.time() - start

# acc data
accuracy = accuracy_score(y_test, y_pred)
print(f"RandomForest 准确率:{accuracy * 100:.3f}%")
recall = recall_score(y_test, y_pred, average='macro')
print(f"RandomForest 召回率:{recall * 100:.3f}%")
f1 = f1_score(y_test, y_pred, average='macro')
print(f"RandomForest F1:{f1 * 100:.3f}%")
print(f"used time:{end_time}")

report = classification_report(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)
print("RandomForest Classification Report:\n")
print(report)
print("RandomForest Confusion Matrix:\n")
print(cm)

# plot heatmap for res visualization
cm = pd.crosstab(y_test, y_pred, rownames=['Actual'], colnames=['Predicted'])
fig, (ax1) = plt.subplots(ncols=1, figsize=(5,5))
sns.heatmap(cm, 
            xticklabels=['Not Fraud', 'Fraud'],
            yticklabels=['Not Fraud', 'Fraud'],
            annot=True,ax=ax1,
            linewidths=.2,linecolor="Darkblue", cmap="Blues")
plt.title('Confusion Matrix', fontsize=14)
plt.show()